package com.unitd.frame.sso.common.helper;

import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @desc Ajax请求响应帮助类
 * @filename AjaxHelper.java
 * @copyright www.unitd.com
 * @author Hudan
 * @version 1.0
 * @date 2016/10/24
 */
public class AjaxHelper {

	/**
	 * @desc 响应JSON内容字符串
	 * @param response http响应对象
	 * @param object   需要相应的并转换成json的对象
	 * @throws IOException
	 */
	public static void jsonPrint(HttpServletResponse response, Object object, String charset) throws IOException {
		outPrint(response, JSON.toJSONString(object), charset);
	}

	/**
	 * @desc 响应Ajax请求
	 * @param response http响应对象
	 * @param content  响应内容
	 * @param charset  字符编码
	 * @throws IOException
	 */
	public static void outPrint(HttpServletResponse response, String content, String charset) throws IOException {
		response.setContentType("text/html;charset=" + charset);
		PrintWriter out = response.getWriter();
		out.print(content);
		out.flush();
	}
}